package megrez.oss.local.domain;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Convert;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.Id;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Version;

import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import megrez.oss.common.Snowflake;

/**
 * 元数据
 * 
 * @author Lucky Yang
 * @since 2.6.9
 */
@Getter
@Setter
@ToString
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
@Entity(name = "oss_metadata")
@EntityListeners({ AuditingEntityListener.class })
public class Metadata {
    @Id
    @EqualsAndHashCode.Include
    @Column(updatable = false, nullable = false, unique = true)
    private String id;

    @Version
    @Column(nullable = false)
    private Integer version;

    @CreatedBy
    @Column(updatable = false)
    private String createdBy;

    @CreatedDate
    @Column(updatable = false)
    @Temporal(TemporalType.TIMESTAMP)
    private Date createdDate;

    /** 名称 */
    private String name;

    /** 文件路径 */
    @Convert(converter = FilePathPairConverter.class)
    private FilePathPair path;

    /** 类型 */
    private String contentType;

    /** 大小，字节 */
    private Long size;

    public Metadata() {
    }

    public Metadata(String name, FilePathPair path, String contentType, long size) {
        this.name = name;
        this.path = path;
        this.contentType = contentType;
        this.size = size;
    }

    @PrePersist
    @PreUpdate
    public void preAction() {
        if (getId() == null) {
            setId(Snowflake.nextIdStr());
        }
    }
   
}
